package com.xiaoxin.nmzp.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxin.nmzp.server.entity.domain.NmzpMessage;
import com.xiaoxin.nmzp.server.entity.domain.NmzpUserTarget;
import com.xiaoxin.nmzp.server.entity.req.MsgReq;
import com.xiaoxin.nmzp.server.entity.vo.MsgItemVO;
import com.xiaoxin.nmzp.server.mapper.NmzpUserTargetMapper;
import com.xiaoxin.nmzp.server.service.NmzpMessageService;
import com.xiaoxin.nmzp.server.mapper.NmzpMessageMapper;
import com.xiaoxin.nmzp.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author xiaoxin
* @description 针对表【nmzp_message】的数据库操作Service实现
* @createDate 2024-11-05 11:21:35
*/
@Service
public class NmzpMessageServiceImpl extends ServiceImpl<NmzpMessageMapper, NmzpMessage>
    implements NmzpMessageService{
    @Autowired
    private NmzpUserTargetMapper nmzpUserTargetMapper;

    @Override
    public void sendMsg(MsgReq msgReq) {
        //建立好友关系
        Integer i = nmzpUserTargetMapper.selectUserTarget(UserUtils.get().getUserId(), msgReq.getReceiverId());
        if (i !=2 ){
            ArrayList<NmzpUserTarget> nmzpUserTargets = new ArrayList<>();
            nmzpUserTargets.add(new NmzpUserTarget(UserUtils.get().getUserId(), msgReq.getReceiverId()));
            nmzpUserTargets.add(new NmzpUserTarget(msgReq.getReceiverId(), UserUtils.get().getUserId()));
            nmzpUserTargetMapper.insert(nmzpUserTargets);
        }
        NmzpMessage nmzpMessage = new NmzpMessage();
        nmzpMessage.setMsg(msgReq.getMsg());
        nmzpMessage.setSenderId(UserUtils.get().getUserId());
        nmzpMessage.setRecipientId(msgReq.getReceiverId());
        nmzpMessage.setCreateTime(new Date());
        save(nmzpMessage);
    }

    /**
     * 获取牛马信息
     *
     * @param targetId 目标用户
     * @return
     */
    @Override
    public List<NmzpMessage> getTargetMsg(Long targetId) {
        return baseMapper.getTargetMsg(UserUtils.get().getUserId(), targetId);
    }

    /**
     * 查询聊天列表
     *
     * @return
     */
    @Override
    public List<MsgItemVO> selectMsgItem() {
        return nmzpUserTargetMapper.selectMsgItem(UserUtils.get().getUserId());
    }
}




